﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LIMS.DALFactory
{
    internal class DefaultCacheAdapter : ICacheStorage
    {
        /// <summary>
        /// http上下文对象
        /// </summary>
        private HttpContext context = null;

        public DefaultCacheAdapter()
        {
            context = HttpContext.Current;
        }

        /// <summary>
        /// 移除指定对象的缓存数据
        /// </summary>
        /// <param name="cacheKey">待移除的缓存键</param>
        public  void Remove(String cacheKey)
        {
            context.Cache.Remove(cacheKey);
        }

        /// <summary>
        /// 添加指定的键值到缓存
        /// </summary>
        /// <param name="cacheKey">待添加的缓存键</param>
        /// <param name="cacheValue">待添加的数据</param>
        public void Add(String key, object data)
        {
            context.Cache.Insert(key, data);
        }

        /// <summary>
        /// 根据指定的键访问对应的缓存数据
        /// </summary>
        /// <typeparam name="T">指定的类型</typeparam>
        /// <param name="cacheKey">访问缓存的键</param>
        /// <returns>返回缓存中的缓存值</returns>
        public T Get<T>(String cachekey)
        {
            var result = (T)context.Cache.Get(cachekey);
            if (result == null)
            {
                result = default(T);
            }
            return result;
        }

    }
}